<template>
  <div>
    <el-button type="primary" icon="el-icon-plus" @click="showDialog"
      >添加管理员</el-button
    >

    <el-dialog
      :title="cform.id ? '修改管理员' : '添加管理员'"
      :visible.sync="dialogFormVisible"
    >
      <el-form :model="cform">
        <el-form-item label="账号" :label-width="formLabelWidth">
          <el-input v-model="cform.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input v-model="cform.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input v-model="cform.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="权限等级" :label-width="formLabelWidth">
          <el-input v-model="cform.roles" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" :label-width="formLabelWidth">
          <el-input v-model="cform.phoneNumber" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" :label-width="formLabelWidth">
          <el-input v-model="cform.email" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrUpdateUser">确 定</el-button>
      </div>
    </el-dialog>

    <div style="margin-top: 15px">
      <el-input
        placeholder="请输入内容"
        v-model="input"
        class="input-with-select"
      >
        <el-select v-model="select" slot="prepend" placeholder="请选择">
          <el-option label="账号" value="1"></el-option>
          <el-option label="手机号" value="2"></el-option>
        </el-select>
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="getUserBy"
        ></el-button>
      </el-input>
    </div>

    <el-table :data="Form" style="width: 100%; margin: 20px 0px" border>
      <el-table-column
        type="index"
        label="序号"
        width="80"
        align="center"
      ></el-table-column>
      <el-table-column prop="username" label="账号"> </el-table-column>
      <el-table-column show-overflow-tooltip prop="password" label="密码">
      </el-table-column>
      <el-table-column prop="roles" label="权限等级"> </el-table-column>
      <el-table-column prop="phoneNumber" label="手机号"> </el-table-column>
      <el-table-column prop="email" label="邮箱"> </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="{ row, $index }">
          <el-button
            type="primary"
            icon="el-icon-edit"
            @click="updateUser(row)"
            circle
          ></el-button>
          <el-popover placement="top" width="160" v-model="visible">
            <p>这是一段内容这是一段内容确定删除吗？</p>
            <div style="text-align: right; margin: 0">
              <!-- <el-button size="mini" type="text" @click="visible = false"
                >取消</el-button
              > -->
              <el-button type="primary" size="mini" @click="deleteUser(row)"
                >确定</el-button
              >
            </div>
            <el-button
              style="margin-left: 15px"
              type="danger"
              icon="el-icon-delete"
              circle
              slot="reference"
            ></el-button>
          </el-popover>
          <!-- <el-button type="danger" icon="el-icon-delete" circle></el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="margin-top: 20px; textalign: center"
      :current-page="pages.page"
      :total="total"
      :page-size="pages.pageSize"
      :pager-count="7"
      :page-sizes="[5, 10, 20]"
      @current-change="getPageList"
      @size-change="handleSizeChange"
      layout="prev,pager,next,jumper,->,total,sizes"
    ></el-pagination>
  </div>
</template>
<script>
export default {
  name: "sysUser",
  data() {
    return {
      dialogFormVisible: false,
      formLabelWidth: "120px",
      total: 0,
      pages: {
        pageSize: 5,
        page: 1,
      },
      cform: {
        nickname: "",
        status: "",
        deteled: "",
        creationTime: "",
        founder: "",
        avatar: "",
        password: "",
        phoneNumber: "",
        roles: "",
        username: "",
        id: "",
        email: "",
      },
      Form: [],
      input: "",
      select: "",
      tableData: "",
      // visible: false,
    };
  },
  mounted() {
    this.getUsrePage();
  },
  methods: {
    async deleteUser(row) {
      let result = await this.$API.user.reqdeleteUser(row);

      if (result.code == 20000) {
        this.getPageList(this.Form.length > 1 ? this.page : this.page - 1);
        this.$message({
          type: "success",
          message: "删除成功",
        });
      }
    },
    getPageList(pager) {
      this.getUsrePage(pager);
    },
    handleSizeChange(pageSize) {
      this.pages.pageSize = pageSize;
      this.getUsrePage();
    },
    updateUser(row) {
      console.log(row);
      this.dialogFormVisible = true;
      this.cform = { ...row };
    },
    showDialog() {
      this.cform = {};
      this.dialogFormVisible = true;
    },
    async getUsrePage(pager = 1) {
      this.pages.page = pager;
      let result = await this.$API.user.reqfindUserPage(this.pages);
      console.log(result,'result')
      if (result.code == 20000) {
        this.Form = result.data.records;
        this.total = result.data.total;
      }
    },
    async getUserBy() {
      if (this.select.length<1){
        this.getUsrePage();
        return;
      }
      let result = await this.$API.user.reqfindUserBy((this.select.length>0?this.select:'***'), (this.input.length>0?this.input:'***'));
      // this.Form = tableData.push(result.data);
      if (result.code == 20000) {
        this.Form = [];
        if (result.data != null) {
          this.Form.push(result.data);
        }
      }
    },
    async addOrUpdateUser() {
      this.dialogFormVisible = false;
      let result = await this.$API.user.reqaddUser(this.cform);
      if (result.code == 20000) {
        this.$message({
          type: "success",
          message: this.cform.id ? "修改管理员" : "添加管理员成功",
        });
        this.getPageList(this.cform.id ? this.pages.page : 1);
      }
    },
  },
};
</script>
<style>
.el-select .el-input {
  width: 130px;
}
.input-with-select .el-input-group__prepend {
  background-color: #fff;
}
</style>
